package com.zrentech.order.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.util.Log;

public class DateTimeHelper {

    private static final String TAG = "DateTimeHelper";

	private static SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS");
	
	/**
	 * 格式化日期，返回日期字符�?	 * @param date 日期
	 * @return 日期字符�?	 */
	public static String dateFormat(Date date){
		return dateformat.format(date);
	}
	
	/**
	 * 返回当前系统时间的日期字符串
	 * @return
	 */
	public static String dateFormat(){
		return dateFormat(new Date());
	}

    // Wed Dec 15 02:53:36 +0000 2010
    public static final DateFormat ESharing_DATE_FORMATTER = new SimpleDateFormat(
            "E MMM d HH:mm:ss Z yyyy", Locale.US);

    public static final DateFormat ESharing_SEARCH_API_DATE_FORMATTER = new SimpleDateFormat(
            "E, d MMM yyyy HH:mm:ss Z", Locale.US); // TODO: Z -> z ?

    public final static DateFormat DB_DATE_FORMATTER = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
    
    public static final Date parseDateTime(String dateString) {
        try {
            Log.v(TAG, String.format("in parseDateTime, dateString=%s", dateString));
            return ESharing_DATE_FORMATTER.parse(dateString);
        } catch (ParseException e) {
            Log.w(TAG, "Could not parse Twitter date string: " + dateString);
            return null;
        }
    }

    // Handle "yyyy-MM-dd'T'HH:mm:ss.SSS" from sqlite
    public static final Date parseDateTimeFromSqlite(String dateString) {
        try {
            Log.v(TAG, String.format("in parseDateTime, dateString=%s", dateString));
            return DB_DATE_FORMATTER.parse(dateString);
        } catch (ParseException e) {
            Log.w(TAG, "Could not parse Twitter date string: " + dateString);
            return null;
        }
    }

    public static final Date parseSearchApiDateTime(String dateString) {
        try {
            return ESharing_SEARCH_API_DATE_FORMATTER.parse(dateString);
        } catch (ParseException e) {
            Log.w(TAG, "Could not parse Twitter search date string: "
                    + dateString);
            return null;
        }
    }

    public static final DateFormat AGO_FULL_DATE_FORMATTER = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm");

    /*public static String getRelativeDate(Date date) {
        Date now = new Date();

        String prefix = ESharingApplication.mContext
                .getString(R.string.tweet_created_at_beautify_prefix);
        String sec = ESharingApplication.mContext
                .getString(R.string.tweet_created_at_beautify_sec);
        String min = ESharingApplication.mContext
                .getString(R.string.tweet_created_at_beautify_min);
        String hour = ESharingApplication.mContext
                .getString(R.string.tweet_created_at_beautify_hour);
        String day = ESharingApplication.mContext
                .getString(R.string.tweet_created_at_beautify_day);
        String suffix = ESharingApplication.mContext
                .getString(R.string.tweet_created_at_beautify_suffix);

        // Seconds.
        long diff = (now.getTime() - date.getTime()) / 1000;

        if (diff < 0) {
            diff = 0;
        }

        if (diff < 60) {
            return diff + sec + suffix;
        }

        // Minutes.
        diff /= 60;

        if (diff < 60) {
            return prefix + diff + min + suffix;
        }

        // Hours.
        diff /= 60;

        if (diff < 24) {
            return prefix + diff + hour + suffix;
        }

        return AGO_FULL_DATE_FORMATTER.format(date);
    }*/

    public static long getNowTime() {
        return Calendar.getInstance().getTime().getTime();
    }
    
	public static String getCreateAt(Date date){
		Calendar c = Calendar.getInstance();
		if(c.get(Calendar.YEAR)-(date.getYear()+1900)>0){
			int i = c.get(Calendar.YEAR)-date.getYear();
			return i+"年前";
		}else if(c.get(Calendar.MONTH)-date.getMonth()>0){
			int i = c.get(Calendar.MONTH)-date.getMonth();
			return i+"月前";
		}else if(c.get(Calendar.DAY_OF_MONTH)-date.getDate()>0){
			int i = c.get(Calendar.DAY_OF_MONTH)-date.getDate();
			return i+"天前"; 
		}else if(c.get(Calendar.HOUR_OF_DAY)-date.getHours()>0){
			int i = c.get(Calendar.HOUR_OF_DAY)-date.getHours();
			return i+"小时"; 
		}else if(c.get(Calendar.MINUTE)-date.getMinutes()>0){
			int i = c.get(Calendar.MINUTE)-date.getMinutes();
			return i+"分钟";
		}else {
			return "刚刚";
		}
	}    
}
